package mycoffee.order.adapter;

import java.util.List;

import mycoffee.order.decoder.CacheManager;
import mycoffee.order.model.Food;
import mycoffee.order.model.Order;
import mycoffee.order.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * @author Tuong
 * 
 */
public class FoodAdapter extends ArrayAdapter<Food> {
	/** Interface to global information about an application environment. */
	protected Context mContext;
	/** used to instantiate layout XML file into its corresponding View objects. */
	LayoutInflater mInfalInflater;
	private CacheManager mCacheManager;
	private Order mCurrentOder;

	public FoodAdapter(Context context, int textViewResourceId,
			List<Food> objects, CacheManager cacheManager, Order currentOrder) {
		super(context, textViewResourceId, objects);
		mCacheManager = cacheManager;
		mCurrentOder = currentOrder;
		mContext = context;
		mInfalInflater = LayoutInflater.from(context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = mInfalInflater.inflate(R.layout.food_item, null);

			viewHolder = new ViewHolder();
			viewHolder.mTvCount = (TextView) convertView
					.findViewById(R.id.food_item_tv_count);
			viewHolder.mTvPrice = (TextView) convertView
					.findViewById(R.id.food_item_tv_price);
			viewHolder.mTvName = (TextView) convertView
					.findViewById(R.id.food_item_tv_name);
			viewHolder.mIvIcon = (ImageView) convertView
					.findViewById(R.id.food_item_iv_icon);
			viewHolder.mFlCountLayout = (FrameLayout) convertView
					.findViewById(R.id.food_item_fl_count_layout);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		// update data.
		Food food = getItem(position);
		if (food != null) {
			viewHolder.mTvName.setText(food.getNameFollowCurrentLanguage());
			viewHolder.mTvPrice.setText("" + food.getPrice());
			String image = food.getImage();
			Bitmap bitmap = null;
			if (mCacheManager != null) {
				bitmap = mCacheManager.getBitmap(image);
			}
			viewHolder.mIvIcon.setImageBitmap(bitmap);

			// udpate order number.
			int amount = 0;
			if (mCurrentOder != null) {
				amount = mCurrentOder.getAmountForFood(food);
			}
			if (amount > 0) {
				viewHolder.mFlCountLayout.setVisibility(View.VISIBLE);
				viewHolder.mTvCount.setText("" + amount);
			} else {
				viewHolder.mFlCountLayout.setVisibility(View.GONE);
			}
		}

		return convertView;
	}

	class ViewHolder {
		TextView mTvCount;
		TextView mTvPrice;
		TextView mTvName;
		ImageView mIvIcon;
		FrameLayout mFlCountLayout;
	}

	public void setCacheManager(CacheManager cacheManager) {
		mCacheManager = cacheManager;

	}

	/**
	 * update the current order.
	 * 
	 * @param order
	 */
	public void setCurrentOrder(Order order) {
		mCurrentOder = order;
		notifyDataSetChanged();
	}

	/**
	 * update amount the has ordered for the food.
	 * 
	 * @param view
	 *            view of the food.
	 * @param pos
	 */
	public void updateAmount(View view, int pos) {
		if (view == null || mCacheManager == null || mCurrentOder == null) {
			return;
		}

		Food food = getItem(pos);
		ViewHolder viewHolder = (ViewHolder) view.getTag();

		int amount = mCurrentOder.getAmountForFood(food);
		if (amount > 0) {
			viewHolder.mFlCountLayout.setVisibility(View.VISIBLE);
			viewHolder.mTvCount.setText("" + amount);
		} else {
			viewHolder.mFlCountLayout.setVisibility(View.GONE);
		}

	}

	/**
	 * update icon of food.
	 * 
	 * @param pos
	 * @param view
	 */
	public void updateImage(int pos, View view) {
		if (view == null || mCacheManager == null || pos < 0
				|| pos >= getCount()) {
			return;
		}

		Food food = getItem(pos);
		if (food == null) {
			return;
		}
		Bitmap bitmap = mCacheManager.getBitmap(food.getImage());

		ViewHolder viewHolder = (ViewHolder) view.getTag();
		viewHolder.mIvIcon.setImageBitmap(bitmap);

	}
}
